SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO





alter     procedure p_assemblytobom
   @productid int
as
begin
  set nocount on
  declare @ibomid int
  exec p_getnewkey 'td_productbomver', @ibomid output

  insert into td_productbomver (bomid,code,name,productid,quantity
    ,state,create_by,check_by,create_time,update_time,defbom) 
    select @ibomid,code+'-'+cast(@ibomid as varchar),name+'-'+cast(@ibomid as varchar),@productid,1
      ,1,0,0,getdate(),getdate(),0
      from td_product where p_id=@productid

  insert into td_productbom (bomid,rawproductid,rawno,rawbomid,rawqty, wasterate,explan
    ,orderno,create_by) 
    select @ibomid,assemblyid,(select count(1) from td_productassembly c where c.productid=a.productid and c.assemblyid<=a.assemblyid),null,quantity, wasterate,null
      ,(select count(1) from td_productassembly c where c.productid=a.productid and c.assemblyid<=a.assemblyid),0
      from td_productassembly a where productid=@productid order by assemblyid
end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

